﻿using System;
using ch02.binary.module;
using ch02.binary.type;

namespace ch02.binary.wasm_reader
{
    using Expr = Object;

    public partial struct WasmReader
    {
        // global sec
        Global[] readGlobalSec()
        {
            var vec = new Global[ReadVarU32()];
            for (var i = 0; i < vec.Length; i++)
            {
                vec[i] = new Global
                {
                    Type = readGlobalType(),
                    Init = readExpr(),
                };
            }

            return vec;
        }

// expr & instruction
        Expr readExpr()
        {
            for (; ReadByte() != 0x0B;)
            {
            }

            return null;
        }

        GlobalType readGlobalType()
        {
            var gt = new GlobalType
            {
                ValType = ReadValType(),
                Mut = ReadByte(),
            };
            switch (gt.Mut)
            {
                case ConstData.MutConst:
                case ConstData.MutVar:
                    break;
                default:
                    Panic.Run("malformed mutability: %d", gt.Mut);
                    break;
            }

            return gt;
        }
    }
}